var page = 1;
var pageOld = 1;
var pageNum = 10;
var pageI = 1;
var pageL = 5;
var blackUserName = '';
var chagePassUserName = '';
var bizDealCountName = '';
var onOrderSXJName = '';
var auTypeName = 'no';
var editUserShow = 'inline';
var changePassShow = 'inline';
var blackShow = 'inline';
var certiShow = 'inline';
var bizDealCountShow = 'inline';
var orderSJShow = 'inline';
var orderXJShow = 'inline';

//权限控制是否显示
if(loginUserType == 1){
    $("#systemAddUserIsShow").show();
    $("#systemAddUserOpenCityShow").show();
    editUserShow = 'inline';
    changePassShow = 'inline';
    blackShow = 'inline';
    certiShow = 'inline';
    bizDealCountShow = 'inline';
    orderSJShow = 'inline';
    orderXJShow = 'inline';
}else if(loginUserType == 2 || loginUserType == 3 || loginUserType == 4){
    $("#systemAddUserIsShow").hide();
    $("#systemAddUserOpenCityShow").hide();
    editUserShow = 'none';
    changePassShow = 'none';
    blackShow = 'none';
    certiShow = 'none';
    bizDealCountShow = 'none';
    orderSJShow = 'none';
    orderXJShow = 'none';
}else if(loginUserType == 5){
    $("#systemAddUserIsShow").hide();
    $("#systemAddUserOpenCityShow").hide();
    editUserShow = 'none';
    changePassShow = 'none';
    blackShow = 'none';
    certiShow = 'inline';
    bizDealCountShow = 'none';
    orderSJShow = 'none';
    orderXJShow = 'none';
}else {
    $("#systemAddUserIsShow").hide();
    $("#systemAddUserOpenCityShow").hide();
}

//根据市查询所有区
var countyFunction = function(city){
    $.ajax({
        url:"cityWeb/findAllAreaByCity.json",
        type:'POST',
        dataType:"json",
        data:{
            city:city
        },
        success:function(result){
            var tem = "<option value='' selected='selected'>区域</option>";
            $.each( result.byOpenCityList, function(index, content){
                tem += "<option value='"+content.area+"'>"+content.area+"</option>";
            });
            $("#county").empty();
            $("#county").append(tem);
            $(".loginBox .loginDiv ul li .loginSelect1:nth-child(3)").css({'margin-right':'0','float':'right','position':'absolute','right':'0','top':'0'});
        }
    });
}

//根据省份查询所有市
var cityFunction = function(province){
    $.ajax({
        url:"cityWeb/findAllCityByProvince.json",
        type:'POST',
        dataType:"json",
        data:{
            province:province
        },
        success:function(result){
            var tem = "<option value='' selected='selected'>城市</option>";
            $.each( result.byOpenCityList, function(index, content){
                tem += "<option value='"+content.city+"'>"+content.city+"</option>";
            });
            $("#city").empty();
            $("#city").append(tem);
            $(".loginBox .loginDiv ul li .loginSelect1:nth-child(3)").css({'margin-right':'0','float':'right','position':'absolute','right':'0','top':'0'});
        }
    });
}

//获取开通省份
var provinceFunction = function(){
    $.ajax({
        url:"cityWeb/findAllProvince.json",
        type:'GET',
        dataType:"json",
        success:function(result){
            var tem = "<option value='' selected='selected'>省份</option>";
            $.each( result.byOpenCityList, function(index, content){
                tem += "<option value='"+content.province+"'>"+content.province+"</option>";
            });
            $("#province").empty();
            $("#province").append(tem);
            $(".loginBox .loginDiv ul li .loginSelect1:nth-child(3)").css({'margin-right':'0','float':'right','position':'absolute','right':'0','top':'0'});
        }
    });
}();

function provinceChange(){
    var options=$("#province option:selected");
    cityFunction(options.val());
}

function cityChange () {
    var options=$("#city option:selected");
    countyFunction(options.val());
}

var systemUserControlFuntion = function () {
    $.ajax({
        url:"usersWeb/findAllUsers.json",
        type:'GET',
        dataType:"json",
        data:{
            page:page,pageNum:pageNum,
            cnName:$("input[name='cnName']").val(),trueName:$("input[name='trueName']").val(),idCard:$("input[name='idCard']").val(),
            userNameWhere:$("input[name='usernameWhere']").val(), province:$("#province option:selected").val(),
            city:$("#city option:selected").val(),area:$("#county option:selected").val()
        },
        success:function(result){
            var tem = "<tr>" +
                "<th width='5%'><label><input id='checkAll' type='checkbox' /></label></th>" +
                "<th>ID</th>" +
                "<th>用户名</th>" +
                "<th>昵称</th>" +
                "<th>真名</th>" +
                "<th>角色</th>" +
                "<th>职业</th>" +
                "<th>省份</th>" +
                "<th>城市</th>" +
                "<th>区域</th>" +
                "<th>注册时间</th>" +
                "<th>订单数量</th>" +
                "<th>预定量</th>" +
                "<th>好评率</th>" +
                "<th>状态</th>" +
                "<th>已通过认证</th>" +
                "<th>所属加盟商</th>" +
                "<th>操作</th>" +
                "</tr>";
            $.each( result.userList, function(index, content){
                tem += "<tr>" +
                    "<td><label><input type='checkbox' name='subBox' class='selectDiv' value='"+content.id+"'/></label></td>" +
                    "<td><p>"+content.id+"</p></td>" +
                    "<td><p>"+content.username+"</p></td>" +
                    "<td><p>"+content.cnName+"</p></td>" +
                    "<td><p>"+content.trueName+"</p></td>" +
                    "<td><p>"+content.diction+"</p></td>" +
                    "<td><p>"+content.occupation+"</p></td>" +
                    "<td><p>"+content.province+"</p></td>" +
                    "<td><p>"+content.city+"</p></td>" +
                    "<td><p>"+content.area+"</p></td>" +
                    "<td><p>"+renderDateStr(content.createTime)+"</p></td>" +
                    "<td><p>"+content.bizDealCount+"</p></td>" +
                    "<td><p>"+content.bizYdCount+"</p></td>" +
                    "<td><p>"+content.bizStar+"</p></td>";
                    if(content.status == 1){
                        tem += "<td><p>正常上架</p></td>";
                    }else if(content.status == 7){
                        tem += "<td><p>未上架</p></td>";
                    }
                tem +=  "<td><p>"+content.authName+"</p></td>" +
                    "<td><p>"+content.agent+"</p></td>" +
                    "<td><p><span class='userSpan'>" +
                    "<a style='display:"+editUserShow+"' href=\"#\" onclick=\"editUser('"+content.username+"');\">编辑</a>" +
                    "<a class='resetPass' style='display:"+changePassShow+"' onclick=\"changePassFunction('"+content.username+"');\">重置密码</a>" +
                    "<a class='disable' style='display:"+blackShow+"' onclick=\"onBlack('"+content.username+"');\">禁用</a>";
                    if(content.userType == 3 || content.userType == 4){
                        tem += "<a href=\"#\" style='display:"+certiShow+"' onclick=\"certi("+content.id+","+content.userType+");\">增加认证</a>";
                    }

                tem += "<a class='orderNum' style='display:"+bizDealCountShow+"' onclick=\"onBizDealCount('"+content.username+"');\">修改订单数</a>" +
                    "<a class='orderSJ' style='display:"+orderSJShow+"' onclick=\"onOrderSJ('"+content.username+"','"+content.authName+"');\">上线</a>" +
                    "<a class='orderXJ' style='display:"+orderXJShow+"' onclick=\"onOrderSJ('"+content.username+"','"+content.authName+"');\">下线</a>" +
                    "</span></p></td>" +
                    "</tr>";
            });
            $("#systemUserControlTable").empty();
            $("#systemUserControlTable").append(tem);

            var pageSum = result.pageSum;
            var numSum = result.numSum;
            var temPage ="<a href='#' onclick='pageFunction("+page+","+page+",\"em\","+pageSum+");'><em>&lt;&lt;</em></a>";

            if(pageSum<=5){
                pageI = 1;
                pageL = pageSum;
            }else if(pageSum>5 && pageSum-pageI < 4 || pageSum-page < 4){
                pageI = pageSum-4;
                pageL = pageSum;
            }else {
                if(page >= 5){
                    if(page > pageOld){
                        if(page-pageI == 4){
                            pageI = page;
                            pageL = page+4;
                        }
                    }else if(page < pageOld){
                        if(pageL-page == 4){
                            pageI = page-4;
                            pageL = page;
                        }
                    }
                }else {
                    pageI = 1;
                    pageL = pageI+4;
                }
            }

            for(var i=pageI;i<=pageL;i++){
                if(page == i){
                    temPage += "<a class='cur' href='#' onclick='pageFunction("+i+","+page+",\"onc\");'>"+i+"</a>";
                }else {
                    temPage += "<a href='#' onclick='pageFunction("+i+","+page+",\"onc\");'>"+i+"</a>";
                }
            }

            temPage += "<a href='#' onclick='pageFunction("+page+","+page+",\"ep\","+pageSum+");'><em>&gt;&gt;</em></a>" +
                "<span>共"+numSum+"条/"+pageSum+"页</span>" +
                "<input class='pageTxt' name='pageValue' type='text' value='' />" +
                "<input class='pageBut' type='button' value='GO' onclick='pageFunction(0,"+page+",\"select\");'/>";

            $("#systemUserControlPage").empty();
            $("#systemUserControlPage").append(temPage);

            //动态加class
            $(".page a,.orderTit a").click(function(){
                $(this).addClass("cur").siblings().removeClass("cur");
            });

            //修改订单数
            $(".orderNum").click(function(){
                $(".deskDiv").css({
                    display:"block",height:$(document).height()
                });
                $(".modifyOrder").css({
                    top:($(window).height()-$(".modifyOrder").height())/2+$(window).scrollTop()+"px",
                    display:"block"
                });
            });

            //上架
            $(".orderSJ,.disableA,.finished,.callOrder").click(function(){
                $(".deskDiv").css({
                    display:"block",height:$(document).height()
                });
                $(".hintsSJ").css({
                    top:($(window).height()-$(".hints").height())/2+$(window).scrollTop()+"px",
                    display:"block"
                });
            });

            //下架
            $(".orderXJ,.disableA,.finished,.callOrder").click(function(){
                $(".deskDiv").css({
                    display:"block",height:$(document).height()
                });
                $(".hintsXJ").css({
                    top:($(window).height()-$(".hints").height())/2+$(window).scrollTop()+"px",
                    display:"block"
                });
            });

            //重置密码
            $(".resetPass,.disableA,.finished,.callOrder").click(function(){
                $(".deskDiv").css({
                    display:"block",height:$(document).height()
                });
                $(".hintsDP").css({
                    top:($(window).height()-$(".hints").height())/2+$(window).scrollTop()+"px",
                    display:"block"
                });
            });

            //禁用用户
            $(".disable").click(function(){
                $(".deskDiv").css({
                    display:"block",height:$(document).height()
                });
                $(".hintsDiv").css({
                    top:($(window).height()-$(".hintsDiv").height())/2+$(window).scrollTop()+"px",
                    display:"block"
                });
            });

            //修改密码
            $(".confirmDP").click(function(){
                closeDivFunction();
                $.ajax({
                    url:"loginWeb/editPassWord.json",
                    type:'POST',
                    dataType:"json",
                    data:{
                        username:chagePassUserName,passwordNew:'12345678'
                    },
                    success:function(result){
                        if(result.isOK == 'OK'){
                            hintsPValueFucntion("修改成功");
                            navPills('systemUserControlHtml',2);
                        }else {
                            hintsPValueFucntion(result.msg);
                        }
                    },
                    error:function(){
                        hintsPValueFucntion("请求失败");
                    }
                });
            });

            //复选框
            $("table tr th label,table tr td label").click(function(){
                $(this).toggleClass("on");
            })

            var dianjixuanzekuang = function() {
                if ($('.selectDiv').length == $('.selectDiv:checked').length) {
                    $('#checkAll').prop({
                        checked : true
                    });
                    $("th label").addClass("on");
                } else {
                    $('#checkAll').prop({
                        checked : false
                    });
                    $("th label").removeClass("on");
                }
            }

            $(".selectDiv").click(function(){
                dianjixuanzekuang();
            });

        }
    });
}

systemUserControlFuntion();

//关闭
$(".cancel,.confirm").click(function(){
    closeDivFunction();
});

//禁用
$(".confirmNo").click(function(){
    closeDivFunction();
});

//获取所有复选框选中返回选中的值
var checkboxValueFunction = function(){
    var str="";
    $('input[name="subBox"]:checked').each(function(index, content){
        if(index == $('input[name="subBox"]:checked').size() - 1){
            str+=$(this).val();
        }else {
            str+=$(this).val()+",";
        }
    })
    return str;
}

//业务权限授权
$(".operation").click(function(){
    if($('input[name="subBox"]:checked').size() != 1){
        hintsPValueFucntion("请选择一条记录操作");
    }else{
        $(".deskDiv").css({
            display:"block",height:$(document).height()
        });
        $(".authority").css({
            top:($(window).height()-$(".authority").height())/2+$(window).scrollTop()+"px",
            display:"block"
        });
        $("[name='checkbox']").removeAttr("checked");//取消全选
        $("input[name='userId']").val(checkboxValueFunction());
        systemOpenCityFunction();
    }
});

//获取开通城市权限
var systemOpenCityFunction = function(){
    var whereI = '';
    if($("input[name='where']").val() != '请输入城市'){
        whereI = $("input[name='where']").val();
    }
    $.ajax({
        url:"cityWeb/findCitiesOpen.json",
        type:'POST',
        dataType:"json",
        data:{
            userId:$("input[name='userId']").val(),
            where:whereI
        },
        success:function(result){
            var tem = "";
            $.each( result.byCityList, function(index, content){
                tem += "<li>" +
                    "<span>"+content.name+"</span>" +
                    "<p class='authP'>";
                var byAreaList = content.byAreaList;
                if(byAreaList != null){
                    $.each(byAreaList, function(index, content){
                        if(content.openType == null){
                            tem += "<label><input type='checkbox' name='cityBox' class='selectDiv' value='"+content.id+"'/>"+content.name+"</label>";
                        }else {
                            tem += "<label class='cur'><input type='checkbox' name='cityBox' class='selectDiv' checked='checked' value='"+content.id+"'/>"+content.name+"</label>";
                        }
                    });
                }
                tem += "</p>" +
                    "</li>";
            });
            $("#systemDictionOpenCity").empty();
            $("#systemDictionOpenCity").append(tem);

            $(".authP label").click(function(){
                $(this).toggleClass("cur");
            });
        },
        error:function(){
            hintsPValueFucntion("请求失败");
        }
    });
}

//保存服务城市
$("#systemDictionOpenCitySubmit").click(function(){
    if($('input[name="cityBox"]:checked').size() > 0){
        var cityBox = $('input[name="cityBox"]:checked');
        var openCitys = '';
        $.each(cityBox, function(index, content){
            openCitys += content.defaultValue + ',';
        });
        openCitys = openCitys.substr(0,openCitys.length-1);
        $.ajax({
            url:"cityWeb/insertRoleOpenCity.json",
            type:'POST',
            dataType:"json",
            data:{
                userId:$("input[name='userId']").val(),
                openCitys:openCitys
            },
            success:function(result){
                if(result.isOK == 'OK'){
                    closeDivFunction();
                    systemUserControlFuntion();
                }
            },
            error:function(){
                hintsPValueFucntion("请求失败");
            }
        });
    }else {
        systemUserControlFuntion();
    }
});

//友情提示
var hintsPValueFucntion = function(v){
    $("#hintsPValue").html(v);
    $(".hints").css({
        top:($(window).height()-$(".hints").height())/2+$(window).scrollTop()+"px",
        display:"block"
    });
}

var pageFunction = function(p,o,t,s){
    if(t == 'select'){
        if(parseInt($("input[name='pageValue']").val()) < 1){
            page = 1;
        }else {
            page = parseInt($("input[name='pageValue']").val());
        }
        pageI = page - 2;
        pageL = page + 2;
    }else if(t == 'em'){
        if(p == 1){
            page = p;
        }else {
            page = p - 1;
        }
        if(p==5 || pageL-p == 4){
            pageI = p-4;
            pageL = p;
        }
    }else if(t == 'ep'){
        if(p == s){
            page = p;
        }else {
            page = p + 1;
        }
        if(p==5 || p-pageI == 4){
            pageI = p;
            pageL = p+4;
        }
    }else {
        page = p ;
    }
    systemUserControlFuntion();
    pageOld = o;
}

//点击禁用，复制禁用username
var onBlack = function(n){
    blackUserName = n;
}

//提交黑名单
var systemBlackSubmit = function(){
    $.ajax({
        url:"usersWeb/updateUserWeb.json",
        type:'POST',
        dataType:"json",
        data:{
            username:blackUserName,status:2,sign:'管理员禁用'
        },
        success:function(result){
            if(result.isOK == 'OK'){
                closeDivFunction();
                systemUserControlFuntion();
            }else {
                hintsPValueFucntion(result.msg);
            }
        }
    });
}

//上下架
var onOrderSJ = function(n,at){
    onOrderSXJName = n;
    auTypeName = at;
}

//上下架订单提交
var onOrderSXJFunction  = function(s){
    if(auTypeName.indexOf("平台认证") >= 0){
        $.ajax({
            url:"usersWeb/updateUserWeb.json",
            type:'POST',
            dataType:"json",
            data:{
                username:onOrderSXJName,
                status:s
            },
            success:function(result){
                if(result.isOK == 'OK'){
                    closeDivFunction();
                    systemUserControlFuntion();
                }else {
                    hintsPValueFucntion(result.msg);
                }
            }
        });
    }else {
        hintsPValueFucntion("未通过平台认证无法上架");
    }
}

//点击修改订单数
var onBizDealCount = function(n){
    bizDealCountName = n;
}

//修改订单数
var bizDealCountFunction = function () {
    if(!isNaN($("input[name='bizDealCount']").val())){
        $.ajax({
            url:"usersWeb/updateUserWeb.json",
            type:'POST',
            dataType:"json",
            data:{
                username:bizDealCountName,
                bizDealCount:$("input[name='bizDealCount']").val()
            },
            success:function(result){
                if(result.isOK == 'OK'){
                    closeDivFunction();
                    systemUserControlFuntion();
                }else {
                    hintsPValueFucntion(result.msg);
                }
            }
        });
    }else {
        hintsPValueFucntion("请输入正确的订单数");
    }
}

//添加用户
$(".addUser").click(function(){
    navPills('systemAddUserHtml');
});

//编辑用户信息
var editUser = function(username){
    addOrUpdateUserName = username;
    navPills('systemEditDataHtml',2);
}

var closeDivFunction = function (){
    $(".deskDiv").fadeOut();
    $(".roleDele").fadeOut();
    $(".roleNew").fadeOut();
    $(".hints").fadeOut();
    $(".hintsDP").fadeOut();
    $(".hintsSJ").fadeOut();
    $(".hintsXJ").fadeOut();
    $(".authority").fadeOut();
    $(".modifyOrder").fadeOut();
    $(".hintsDiv").fadeOut();
    $(".teamDiv").fadeOut();
    $(".refundDiv").fadeOut();
    $(".addNote").fadeOut();
    $(".evaluation").fadeOut();
}

//重置密码记录账号
var changePassFunction = function (u) {
    chagePassUserName = u;
}

//添加认证
var certi = function(i,userType){
    $(".mainDiv").load('htmlWeb/systemApplyCertiHtml', '', function () {
        $("#userId").val(i);
        $("#businessType").val(userType);
        $.getScript("../../by-webhtml/assets/js/system/system_applyCerti.js");
    });
}